package com.wx.activitys.drp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.activitys.base.utils.Common;
import com.wx.activitys.drp.EnumUtils.WithdrawStateType;
import com.wx.activitys.drp.Utils.DoubleUtil;
import com.wx.activitys.drp.mapper.BWithdrawalMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.model.BWithdrawal;
import com.wx.activitys.drp.modelExample.BWithdrawalExample;
import com.wx.activitys.drp.service.BWithdrawalService;
@Service("/bWithdrawalImpl")
public class BWithdrawalImpl implements BWithdrawalService {

	@Autowired
	BWithdrawalMapper bWithdrawalMapper;
	@Autowired
	UserInfoMapper userInfoMapper;
	
	
	@Override
	public int insertWithdrawal(BWithdrawal bWithdrawal,Double sub) {
		Map<String,Object> paramMap = new HashMap<>();
     	bWithdrawal.setId(Common.generateUUID());
     	bWithdrawal.setCreateTime(new Date());
     	bWithdrawal.setState(WithdrawStateType.处理中.getValue());
     	paramMap.put("openid", bWithdrawal.getOpenid());
     	paramMap.put("incomeSub",sub);
     	userInfoMapper.update(paramMap);
		return bWithdrawalMapper.insertWithdrawal(bWithdrawal);
	}

	@Override
	public List<BWithdrawalExample> queryPageList(BWithdrawal entity) {
		return bWithdrawalMapper.queryPageList(entity);
	}

	@Override
	public int count(BWithdrawal entity) {
		// TODO Auto-generated method stub
		return bWithdrawalMapper.count(entity);
	}

	@Override
	public int update(BWithdrawal bWithdrawal) {
		BWithdrawal entity = new BWithdrawal();
		entity.setId(bWithdrawal.getId());
		List<BWithdrawalExample> BWithdrawal = bWithdrawalMapper.queryPageList(entity);
		BUserInfo buserInfo = userInfoMapper.findByOpenid(BWithdrawal.get(0).getOpenid());
		Double sub = DoubleUtil.add(buserInfo.getIncome(), BWithdrawal.get(0).getNumber());
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("openid", buserInfo.getOpenid());
     	paramMap.put("incomeSub",sub);
     	userInfoMapper.update(paramMap);
		return bWithdrawalMapper.update(bWithdrawal);
	}

	@Override
	public List<BWithdrawal> findByEntity(Map<String,Object> map) {
		// TODO Auto-generated method stub
		return bWithdrawalMapper.findByEntity(map);
	}

	@Override
	public int countByMap(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return bWithdrawalMapper.countByMap(map);
	}
}
